---
--GamersArmy.net Shared code 
--TTT GA mod
---

--set Models we can pick from, i know we have to set up a 4 tables for this make sure you set them all.
--the model
gamod= {}
gamod[1]=""
gamod[2]="models/player/combine_soldier.mdl"
gamod[3]="models/player/alyx.mdl"
gamod[4]="models/player/gman_high.mdl"
gamod[5]="models/player/monk.mdl"
gamod[6]="models/player/blockdude.mdl"
gamod[7]="models/player/billymays.mdl"
gamod[8]="models/player/slow/arkham_asylum/harley_quinn/gmod_reskin.mdl"
gamod[9]="models/player/kleiner.mdl"
gamod[10]="models/characters/smith.mdl"
gamod[11]="models/characters/pyramid_head.mdl"
gamod[12]="models/apocmodels/deadpool/dyadyapoolya36.mdl"
gamod[13]="models/player/lich_king_wow_maskless.mdl"
gamod[14]="models/vinrax/player/jack_player.mdl"
--set prices
gamodprice= {}
gamodprice[1]="0"
gamodprice[2]="100"
gamodprice[3]="125"
gamodprice[4]="200"
gamodprice[5]="220"
gamodprice[6]="5550"
gamodprice[7]="1200"
gamodprice[8]="1225"
gamodprice[9]="100"
gamodprice[10]="1600"
gamodprice[11]="1620"
gamodprice[12]="2750"
gamodprice[13]="2500"
gamodprice[14]="75000"
--name to ID
gamodnamechat= {}
gamodnamechat["none"]=1
gamodnamechat["soldier"]=2
gamodnamechat["alyx"]=3
gamodnamechat["gman"]=4
gamodnamechat["monk"]=5
gamodnamechat["steve"]=6
gamodnamechat["billy"]=7
gamodnamechat["harley_quinn"]=8
gamodnamechat["kleiner"]=9
gamodnamechat["smith"]=10
gamodnamechat["Pyrmid Head"]=11
gamodnamechat["Deadpool"]=12
gamodnamechat["Lich King"]=13
gamodnamechat["Jack"]=14
--ID to name
gamodnamelocal= {}
gamodnamelocal[0]="none"
gamodnamelocal[1]="none"
gamodnamelocal[2]="soldier"
gamodnamelocal[3]="alyx"
gamodnamelocal[4]="gman"
gamodnamelocal[5]="monk"
gamodnamelocal[6]="steve"
gamodnamelocal[7]="billy"
gamodnamelocal[8]="harley_quinn"
gamodnamelocal[9]="kleiner"
gamodnamelocal[10]="smith"
gamodnamelocal[11]="Pyrmid Head"
gamodnamelocal[12]="Deadpool"
gamodnamelocal[13]="Lich King"
gamodnamelocal[14]="Jack"
--buy class
gabuy = {}

gaTcost = "400"
gaDcost = "200"

greencolor = Color( 25, 200, 25 )
whitecolor = Color( 255, 255, 255 )
redcolor = Color( 255, 25, 25 )
 
local function updateNextround()
	table.Empty( gabuy ) -- Don't reassign so we don't lose our refs  
    table.insert(gabuy,"traitor") -- Add "traitor" to the table.
    table.insert(gabuy,"detective") -- Add "detective" to the table.	
    table.insert(gabuy,"unmark") -- Add "unmark" to the table.

end
hook.Add( ULib.HOOK_UCLCHANGED, "ULXNextRoundUpdate", updateNextround )
updateNextround() -- Init


GaPlysMarkedForTraitor = {}
GaPlysMarkedForDetective = {}
function gabuyclass( target_plys, next_round )
  --ULib.tsayError( target_plys, "buy test", true )
  --ULib.tsayError( target_plys, next_round, true )
    money = target_plys:GetNWInt("money")
	print(money)
	money = tonumber(money)
    local affected_plys = {}
	local unaffected_plys = {}
        local v = target_plys
        local ID = v:UniqueID()
        
        if next_round == "traitor" then
		    if money > tonumber(gaTcost) then
                if GaPlysMarkedForTraitor[ID] == true or GaPlysMarkedForDetective[ID] == true then
                    ULib.tsayError( target_plys, "You are already paid for this!", true )
                else
				    spendMoney( target_plays, tonumber(gaTcost))
                    GaPlysMarkedForTraitor[ID] = true
                    table.insert( affected_plys, v ) 
                end
			else
			    ULib.tsayError( target_plys, "You don't have enough money!", true )
			end
        end
        if next_round == "detective" then
		    if money > tonumber(gaDcost) then
                if GaPlysMarkedForTraitor[ID] == true or GaPlysMarkedForDetective[ID] == true then
                    ULib.tsayError( target_plys, "You are already paid for this!", true )
                else
				    spendMoney( target_plays, tonumber(gaDcost))
			        ULib.tsayError( target_plys, "buy Dect", true )
                    GaPlysMarkedForDetective[ID] = true
                    table.insert( affected_plys, v ) 
                end
			else
			   ULib.tsayError( target_plys, "You don't have enough money!", true )
			end
        end  
end        

function spendMoney (ply, spend)
    if CLIENT then 
	ply = LocalPlayer()
	end
	local spend = string.Explode(" ", spend)
    spend = spend[1]
	print("spend = ".. spend)
    local money = ply:GetNWInt("money")
	unique_id = ply:GetNWString ("SteamID")
    money = money - spend
	ply:SetNWInt("money", money)
    ULib.tsayError(ply, "You now have $"..money, true )
	RunConsoleCommand( "savestat", money )	
end


function GaTraitorMarkedPlayers()
	for k, v in pairs(GaPlysMarkedForTraitor) do
		if v then
			ply = player.GetByUniqueID(k)
			RunConsoleCommand( "ga2", k)
			GaPlysMarkedForTraitor[k] = false
		end
	end
end
hook.Add("TTTBeginRound", "GaT", GaTraitorMarkedPlayers)

function GaDetectiveMarkedPlayers()
	for k, v in pairs(GaPlysMarkedForDetective) do
		if v then
		    ply = player.GetByUniqueID(k)
			RunConsoleCommand( "ga1", k)
			GaPlysMarkedForDetective[k] = false
		end
	end
end
hook.Add("TTTBeginRound", "GaD", GaDetectiveMarkedPlayers)


---WEAPON BUYS---

weaps = {}
-- Add weapons to store
weaps[1] = {
					name = "Deagle",
					swep = "weapon_zm_revolver",
					prices = {
						reg = "50",
						dic = "30",
					},
					slot = "1",
					tonly = false,
				}
weaps[2] = {
				name = "Galil",
				swep = "weapon_ttt_galil",
				prices = {
					reg = "100",
					dic = "60",
				},
				slot = "2",
				tonly = false,
				}				
weaps[3] = {
				name = "S.L.A.M",
				swep = "weapon_ttt_slam",
				prices = {
					reg = "300",
					dic = "100",
				},
				slot = "7",
				tonly = true,
				}	

-- Weapon data functions				
function getWeap( name )
	if weaps[name] then
		return weaps[name];
	end
	return false
end	

function totalWeap()
	print(table.Count(weaps))
	for i=1,table.Count(weaps) do
	print("-----Weapon----")
	print(weaps[i].name)
	print("Normal cost: $"..weaps[i].prices.reg)
	print("Discount cost: $"..weaps[i].prices.dic)
	end
end

concommand.Add("weap", totalWeap)	
				
--betting crap
function AlivePerc()
local Players = player.GetAll()
local Playerstot = table.Count(Players) 
local Playersalive = 0
   local players = player.GetAll()
	for _, ply in ipairs( players ) do
		if ply:Alive() and (not ply:IsSpec()) then
			Playersalive = Playersalive + 1
		end
	end
local perc = Playersalive / Playerstot
perc = perc * 100
return perc
end
betting = "inactive"
function Betstart() 
	local round = GetRoundState()
	if round == 3 then 
		if AlivePerc() <= 90 and  AlivePerc() >= 0 then		
			if betting == "active" then	
			else
				local players = player.GetAll()
				for _, ply in ipairs( players ) do
					if not ply:Alive() then						
						ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " A END ROUND BET HAS STARTED!")
						ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " IT IS TIME TO PICK WHAT TEAM YOU WILL THINK WINS!")
						ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " TO VOTE FOR THE TRAITORS SAY !bet t")
						ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " TO VOTE FOR THE INNOCENT SAY !bet i")
						ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " HAPPY BETTING")
					end
				end
			end
			betting = "active"
		else 
			betting = "inactive"
			--print("Not enough people to start a bet!")
		end
	else
	betting = "inactive"
	print("Not in a round we can bet in!")
	end

end
function BetRound(wintype)
	
	if SERVER then
		if wintype == WIN_INNOCENT then
		bwinteam = "innocent"
		elseif wintype == WIN_TRAITOR then
		bwinteam = "traitors"
		else
		bwinteam = "none"
		ULib.tsayColor(nil, nil, redcolor, "PAYOUT ", whitecolor, "No one won!")
		end
	local players = player.GetAll()
		for _, ply in ipairs( players ) do
			SteamID = ply:SteamID()
			name = ply:Nick()
			if bets[SteamID] then 
				if bets[SteamID].bteam == bwinteam then
					payout = bets[SteamID].amount * 1.6
					ULib.tsayColor(nil, nil, redcolor, "PAYOUT ", whitecolor, "Well, "..name.." is seems you won your bet! And you got a payout of $"..payout)
					addMoneyquite(ply,payout)
				end
			end
		end
	end
	bets = {}
end

bets = {}
local ibets = {}
local maxbet = 80
function BetChat (ply, text) 
	local round = GetRoundState()
	local txt = string.Explode(" ", text)
	SteamID = ply:SteamID()
	if (txt[1] == "!bet") then
		if ply:Alive() then
			ULib.tsayColor(ply, nil, redcolor, "BET", whitecolor, " You can only bet when you are dead")
		return false
		end
		money = ply:GetNWInt("money")
		money = tonumber(money)
		if round == 3 then
			if betting == "active" then
				if bets[SteamID] then
					ULib.tsayColor(ply, nil, redcolor, "BET", whitecolor, " You already played your bet. You bet for "..bets[SteamID].bteam)
				else
					
					if (not txt[3]) then
						betamout = "20"
					else
						if tonumber(txt[3]) then
							betamout = tonumber(txt[3])
						else
							ULib.tsayColor(ply, nil, redcolor, "BET", whitecolor, " That is not a number!")
							return false
						end
					end
				    if tonumber(betamout) <= 0 then
					    ULib.tsayColor(ply, nil, redcolor, "BET", whitecolor, " You cant bet neg numbers.. asshole..")
					return false 
					end
					if  tonumber(money) < tonumber(betamout) then
						ULib.tsayColor(ply, nil, redcolor, "BET", whitecolor, " YOU ARE POOR!")
						return false
					end
					if tonumber(betamout) > tonumber(maxbet) then
					ULib.tsayColor(ply, nil, redcolor, "BET", whitecolor, " You can not bet more than the max of $"..maxbet)
					return false
					end
					if (txt[2] == "i") then
						bets[SteamID] = {
								bteam ="innocent",
								amount = betamout,
								}
						spendMoney( ply, betamout)
						saveStat(ply)
						ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " You voted for the INNOCENT! With a bet of $"..betamout)
					elseif(txt[2] == "t") then
						bets[SteamID] = {
								bteam ="traitors",
								amount = betamout,
								}
						spendMoney( ply, betamout)
						saveStat(ply)
						ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " You voted for the TRAITORS! With a bet of $"..betamout)
					else
						ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " You need to pick the winning team.")
					end
				end
				--ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " We CAN bet here...")
			else
				ULib.tsayColor(ply, nil, greencolor, "BET", whitecolor, " We don't have a bet started yet...")
			end
		else
		ULib.tsayColor(ply, nil, redcolor, "BET", whitecolor, " You can only bet when a game is active")
		end
	end
end


hook.Add("PlayerSay", "BetChat", BetChat)
hook.Add( "PlayerDeath", "BetCall", Betstart )
hook.Add("TTTEndRound", "BetEndround", BetRound )
concommand.Add("bettest", Betstart)
concommand.Add("perctest", AlivePerc)						
